package com.ahui.ydd.controller;

import com.ahui.ydd.Vo.YddAdvimgVo;
import com.ahui.ydd.common.R;
import com.ahui.ydd.dto.YddAdvimgDto;
import com.ahui.ydd.entity.YddAdvimg;
import com.ahui.ydd.page.YddAdvimgPage;
import com.ahui.ydd.service.YddAdvimgService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ydd_advimg")
@Slf4j
public class YddAdvimgController {

    @Autowired
    private YddAdvimgService yddAdvimgService;

    /**
     * 广告图详情(用于修改回显数据)
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddAdvimg> rowInfo(int id){
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        return R.success(yddAdvimg);
    }

    /**
     * 广告图分页查询
     * @param yddAdvimgDto
     * @return
     */
    @GetMapping("/list")
    public R<YddAdvimgPage> list(YddAdvimgDto yddAdvimgDto){
        //分页拦截器
        Page<YddAdvimg> page =new Page<>(yddAdvimgDto.getPage(),yddAdvimgDto.getLimit());
        //条件构造器
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(yddAdvimgDto.getAdvimgdesc()),YddAdvimg::getAdvimgdesc,yddAdvimgDto.getAdvimgdesc());
        queryWrapper.eq(StringUtils.isNotEmpty(yddAdvimgDto.getAdvimgpos()),YddAdvimg::getAdvimgpos,yddAdvimgDto.getAdvimgpos());
        //查询
        Page<YddAdvimgVo> yddAdvimgVoPage = yddAdvimgService.pageByCondition(page, queryWrapper);
        //返回数据
        YddAdvimgPage yddAdvimgPage = new YddAdvimgPage();
        yddAdvimgPage.setCount(yddAdvimgVoPage.getTotal());
        yddAdvimgPage.setCurrentPage(yddAdvimgVoPage.getCurrent());
        yddAdvimgPage.setData(yddAdvimgVoPage.getRecords());
        yddAdvimgPage.setPageSize(yddAdvimgVoPage.getSize());
        yddAdvimgPage.setTotalPages(yddAdvimgVoPage.getPages());
        return R.success(yddAdvimgPage);
    }

    /**
     * 广告图修改
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvimg yddAdvimg){
        yddAdvimgService.save(yddAdvimg);
        return R.success("广告图修改成功");
    }

    /**
     * 广告图删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> del(int id){
        yddAdvimgService.removeById(id);
        return R.success("广告图删除成功");
    }

    /**
     * 广告图添加
     * @param yddAdvimg
     * @return
     */
    @GetMapping("/add")
    public R<String> add(@RequestBody YddAdvimg yddAdvimg){
        yddAdvimgService.save(yddAdvimg);
        return R.success("广告图添加成功");
    }
}
